﻿using Common.EF.Controllers;
using Common.Utility;
using Common.ViewModels;
using Stylet;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Controls;
using System.Windows.Threading;
using System.Windows;
using AWSV2.ViewModels;
using System.Threading;

namespace AWSV2.ViewModels
{
    public class DataManagementShellViewModel : Screen
    {
        public event EventHandler Closed;

        /// <summary>
        /// 控件初始化之后赋值 --阿吉 2025年1月17日20点44分
        /// </summary>
        private Window _windowIns;

        public void WindowLoaded(object sender, RoutedEventArgs e)
        {
            _windowIns = (Window)sender;
        }

        private IWindowManager _windowManager;
        private IEventAggregator _eventAggregator;

        public UserViewModel UserViewModel { get; set; }
        public RoleViewModel RoleViewModel { get; set; }
        public CustomerViewModel CustomerViewModel { get; set; }
        public GoodsViewModel GoodsViewModel { get; set; }
        public CarViewModel CarViewModel { get; set; }
        public ICViewModel ICViewModel { get; set; }
        public SysFieldViewModel SysFieldViewModel { get; set; }

        public WeighingRecordRelationViewModel WeighingRecordRelationViewModel { get; set; }

        public bool IsVisible { get; set; } = false;

        public bool EnableUser { get; set; } = false;
        public bool EnableRole { get; set; } = false;
        public bool EnableCustomer { get; set; } = false;
        public bool EnableCar { get; set; } = false;
        public bool EnableGoods { get; set; } = false;
        public bool EnableIC { get; set; } = false;
        public bool EnableSysField { get; set; } = false;

        public bool EnableWeighingRecordRelation { get; set; }

        public string KhName
        {
            get
            {
                //var kh = DataManagement.Globalspace.FormCols?.Find(p => p.Key == "_kh");
                //var name = kh == null ? "客户" : kh.Value;
                //return $"{name}管理";
                return "客户管理";
            }
        }

        public string WzName
        {
            get
            {
                //var kh = Globalspace.FormCols?.Find(p => p.Key == "_wz");
                //var name = kh == null ? "物资" : kh.Value;
                //return $"{name}管理";
                return "物资管理";
            }
        }

        public string ProvicerTitle
        {
            get
            {
                return Globalspace.ProvicerTitle;
            }
        }

        public DataManagementShellViewModel(IWindowManager windowManager, IEventAggregator eventAggregator)
        {
            _windowManager = windowManager;
            _eventAggregator = eventAggregator;
            var timer = new DispatcherTimer
            {
                Interval = TimeSpan.FromSeconds(1.5)
            };
            timer.Tick += (_, __) =>
            {
                timer.Stop();
                Prepare();
            };
            timer.Start();
        }

        protected override void OnClose()
        {
            base.OnClose();
            Closed?.Invoke(this, new EventArgs());
        }

        public void ActiveWindow()
        {
            _windowIns?.Activate();
        }

        private void Prepare()
        {

            if (Globalspace._currentUser.LoginId != "admin")
            {
                var rolePermission = Globalspace._currentUser?.Permission ?? string.Empty;


                if (!string.IsNullOrWhiteSpace(rolePermission))
                {
                    if (rolePermission.Contains("用户管理"))
                    {
                        EnableUser = true;

                    }
                    if (rolePermission.Contains("角色管理"))
                    {
                        EnableRole = true;

                    }
                    if (rolePermission.Contains("客户管理"))
                    {
                        EnableCustomer = true;

                    }
                    if (rolePermission.Contains("车辆管理"))
                    {
                        EnableCar = true;

                    }
                    if (rolePermission.Contains("物资管理"))
                    {
                        EnableGoods = true;

                    }
                    if (rolePermission.Contains("IC卡管理"))
                    {
                        EnableIC = true;

                    }
                    if (rolePermission.Contains("系统字段"))
                    {
                        EnableSysField = true;
                    }
                    if (rolePermission.Contains("磅单关联"))
                    {
                        EnableWeighingRecordRelation = true;
                    }
                }
            }
            else
            {
                EnableUser = EnableRole = EnableCustomer = EnableCar = EnableGoods = EnableIC = EnableSysField = EnableWeighingRecordRelation = true;
            }

            if (EnableUser && UserViewModel == null)
            {
                UserViewModel = new UserViewModel(_windowManager);
                UserViewModel.ExecuteGetListCmd(null);
            }
        }

        public void TabSelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (!AJDatabaseService.Connected)
            {
                return;
            }
            if (e.OriginalSource is TabControl && ((TabControl)sender).SelectedItem is TabItem tabItem)
            {
                var tag = tabItem.Tag.ToString();
                switch (tag)
                {
                    case nameof(UserViewModel):
                        if (UserViewModel == null)
                        {
                            UserViewModel = new UserViewModel(_windowManager);
                        }
                        UserViewModel.ExecuteGetListCmd(null);
                        break;
                    case nameof(RoleViewModel):
                        if (RoleViewModel == null)
                        {
                            RoleViewModel = new RoleViewModel(_windowManager);
                        }
                        RoleViewModel.ExecuteGetListCmd(null);
                        break;
                    case nameof(CustomerViewModel):
                        if (CustomerViewModel == null)
                        {
                            CustomerViewModel = new CustomerViewModel(_windowManager);
                        }
                        CustomerViewModel.ExecuteGetListCmd(null);
                        break;
                    case nameof(GoodsViewModel):
                        if (GoodsViewModel == null)
                        {
                            GoodsViewModel = new GoodsViewModel(_windowManager);
                        }
                        GoodsViewModel.ExecuteGetListCmd(null);
                        break;
                    case nameof(CarViewModel):
                        if (CarViewModel == null)
                        {
                            CarViewModel = new CarViewModel(_windowManager, _eventAggregator);
                        }
                        CarViewModel.ExecuteGetListCmd(null);
                        break;
                    case nameof(ICViewModel):
                        if (ICViewModel == null)
                        {
                            ICViewModel = new ICViewModel(_windowManager);
                        }
                        ICViewModel.ExecuteGetListCmd(null);
                        break;
                    case nameof(SysFieldViewModel):
                        if (SysFieldViewModel == null)
                        {
                            SysFieldViewModel = new SysFieldViewModel(_windowManager);
                        }
                        SysFieldViewModel.ExecuteGetListCmd(null);
                        break;
                    case nameof(WeighingRecordRelationViewModel):
                        if (WeighingRecordRelationViewModel == null)
                        {
                            WeighingRecordRelationViewModel = new WeighingRecordRelationViewModel(_windowManager, _eventAggregator);
                        }
                        WeighingRecordRelationViewModel.ExecuteGetListCmd(null);
                        break;
                    default:
                        break;
                }

            }
            e.Handled = true;
        }
    }
}
